package GUIv2;

import general.FileUtils;
import general.GlobalProperties;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

import org.apache.log4j.Logger;

import com.jgoodies.looks.plastic.Plastic3DLookAndFeel;

public class GlobalPropertiesGUI extends javax.swing.JFrame {
    private static final long serialVersionUID = 1L;
    private static Logger log = Logger.getLogger(GlobalPropertiesGUI.class);

    public GlobalPropertiesGUI() {
	try {
	    initComponents();
	} catch (NoSuchAlgorithmException e) {
	    log.error(e.getMessage());
	}
    }

    @SuppressWarnings({ "unchecked", "serial" })
    private void initComponents() throws NoSuchAlgorithmException {

	try {
	      UIManager.setLookAndFeel(new Plastic3DLookAndFeel());
	} catch (Exception e) {
	    e.printStackTrace();
	    log.error(e.getMessage());
	}

	cancelButton = new javax.swing.JButton();
	okButton = new javax.swing.JButton();
	jScrollPane1 = new javax.swing.JScrollPane();

	jTable1 = new javax.swing.JTable();

	setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
	setForeground(java.awt.Color.white);
	setName("frame");

	cancelButton.setText("Cancel");
	cancelButton.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButton1ActionPerformed(evt);
	    }
	});

	okButton.setText("Save");
	okButton.addMouseListener(new java.awt.event.MouseAdapter() {
	    public void mouseClicked(java.awt.event.MouseEvent evt) {
		jButton2MouseClicked(evt);
	    }
	});
	okButton.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButton2ActionPerformed(evt);
	    }
	});

	jTable1.setModel(new javax.swing.table.DefaultTableModel(
		new Object[][] {
			{ "username", GlobalProperties.getUsername() },
			{ "pass", FileUtils.MD5(GlobalProperties.getPassword()) },
			{ "timer", GlobalProperties.getTimer() },
			{ "folder moni", GlobalProperties.getFolderMonit() },
			{ "profiles", GlobalProperties.getProfiles() },
			{ "session id", GlobalProperties.getSessionKey() } },
		new String[] { "Nazwa", "Wartosc" }) {
	    @SuppressWarnings("rawtypes")
	    Class[] types = new Class[] { String.class,
		    String.class };
	    boolean[] canEdit = new boolean[] { false, true };

	    @SuppressWarnings("rawtypes")
	    public Class getColumnClass(int columnIndex) {
		return types[columnIndex];
	    }

	    public boolean isCellEditable(int rowIndex, int columnIndex) {
		return canEdit[columnIndex];
	    }
	});
	jScrollPane1.setViewportView(jTable1);

	jScrollPane1.setOpaque(false);
	jScrollPane1.getViewport().setOpaque(false);
	jTable1.setOpaque(false);

	javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
		getContentPane());
	getContentPane().setLayout(layout);
	layout.setHorizontalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			javax.swing.GroupLayout.Alignment.TRAILING,
			layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.TRAILING)
						.addComponent(
							jScrollPane1,
							javax.swing.GroupLayout.DEFAULT_SIZE,
							380, Short.MAX_VALUE)
						.addGroup(
							layout.createSequentialGroup()
								.addGap(0,
									0,
									Short.MAX_VALUE)
								.addComponent(
									cancelButton)
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(
									okButton)))
				.addContainerGap()));
	layout.setVerticalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			javax.swing.GroupLayout.Alignment.TRAILING,
			layout.createSequentialGroup()
				.addContainerGap()
				.addComponent(jScrollPane1,
					javax.swing.GroupLayout.PREFERRED_SIZE,
					264,
					javax.swing.GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(
					javax.swing.LayoutStyle.ComponentPlacement.RELATED,
					javax.swing.GroupLayout.DEFAULT_SIZE,
					Short.MAX_VALUE)
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.BASELINE)
						.addComponent(cancelButton)
						.addComponent(okButton))
				.addContainerGap()));

	okButton.getAccessibleContext().setAccessibleName("okButton");

	pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

	this.setVisible(false);
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
	this.setVisible(false);
    }

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
	Map<String, String> mapa = new HashMap<String, String>();
	for (int i = 1; i < jTable1.getRowCount(); i++) {
	    String a = String.valueOf(jTable1.getValueAt(i, 0));
	    String b = String.valueOf(jTable1.getValueAt(i, 1));
	    mapa.put(a, b);
	}

	FileOutputStream fos = null;
	try {
	    // File propFile = new File(GlobalProperties.myurl.getFile());
	    File propFile = GlobalProperties.globalPropertiesFile;

	    fos = new FileOutputStream(propFile);
	    Properties properties = GlobalProperties.properties;

	    for (String key : mapa.keySet())
		properties.setProperty(key, mapa.get(key));

	    properties.store(fos, "");
	} catch (Exception e) {
	    log.error(e.getMessage());
	} finally {
	    try {
		if (fos != null)
		    fos.close();
	    } catch (IOException e) {
		log.error(e.getMessage());
	    }
	}

	JOptionPane
		.showMessageDialog(null,
			"Niektore zmiany beda widoczne dopiero po ponownym uruchomieniu programu!");
	this.setVisible(false);
    }

    javax.swing.JButton cancelButton;
    javax.swing.JButton okButton;
    javax.swing.JScrollPane jScrollPane1;
    javax.swing.JTable jTable1;
}
